﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Resources;
using System.Web;

namespace WeProjects.Helps
{
    public class Utility
    {
        public static void RemoveBelongs<T>(ICollection<T> collection,WeProjectsModel.ProjectsDBEntities db)
        {
            var list = collection.ToList();
            foreach (var item in list)
            {
                collection.Remove(item);
            }
            db.SaveChanges();
        }
        public static byte[] GetFile(string s)
        {

            System.IO.FileStream fs = System.IO.File.OpenRead(s);
            byte[] data = new byte[fs.Length];
            int br = fs.Read(data, 0, data.Length);
            if (br != fs.Length)
                throw new System.IO.IOException(s);
            return data;
        }
        public static string Translate<T>(string name)
        {
            //ResourceSet resourceSet = PessResources.Archive.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
            ResourceSet resourceSet = new ResourceManager(typeof(T)).GetResourceSet(CultureInfo.CurrentUICulture, true, true);

            foreach (DictionaryEntry entry in resourceSet)
            {
                object resourceKey = entry.Key;
                object resource = entry.Value;
                if ((string)resourceKey == name)
                {
                    return (string)resource;
                }
            }
            return name;
        }
        public static DateTime IgnoreTime(DateTime due)
        {
            if(due!=null)
            {
                due=DateTime.Parse(due.ToShortDateString());
            }
            return due;
        }

        public static string GetImage64String(string path)
        {            
            //var path = Path.Combine(Server.MapPath("~/App_Data/NewsPhotos"), news.ID.ToString(), news.ID.ToString() + ".jpg");
            //string path = Server.MapPath("~/images/computer.png");
            if (File.Exists(path))
            {
                byte[] imageByteData = System.IO.File.ReadAllBytes(path);
                string imageBase64Data = Convert.ToBase64String(imageByteData);
                string imageData = string.Format("data:image/jpg;base64,{0}", imageBase64Data);
                return imageData;
            }
            else
            { return ""; }
        }
    }
}